package com.archie.zhufengfm;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.archie.zhufengfm.fragments.home.CustomTingFragment;
import com.archie.zhufengfm.fragments.home.DiscoverFragment;
import com.archie.zhufengfm.fragments.home.DownloadTingFragment;
import com.archie.zhufengfm.fragments.home.PersonalFragment;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    // 主界面中第一层 Fragment，发现，定制听，下载听，我
    private Fragment[] fragments;

    // 主界面中第一层 加载主要Fragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // TODO 检查横竖屏切换的时候，这个数组对象和内部元素是否置空
        fragments = new Fragment[4];

        FragmentManager manager = getSupportFragmentManager();

        FragmentTransaction tx = manager.beginTransaction();

        if(savedInstanceState != null){
            // 代表Activity 重新创建，内部的Fragment自动添加
            int len = fragments.length;
            for (int i = 0; i < len; i++) {

                fragments[i] = manager.findFragmentByTag("f" + i);
                tx.hide(fragments[i]);

            }

        }else {

            fragments[0] = new DiscoverFragment();
            fragments[1] = new CustomTingFragment();
            fragments[2] = new DownloadTingFragment();
            fragments[3] = new PersonalFragment();


            int length = fragments.length;
            for (int i = 0; i < length; i++) {

                // 添加Fragment，并且指定Tag，在Activity重新创建的时候查找
                tx.add(R.id.main_fragment_container, fragments[i], "f" + i);
                tx.hide(fragments[i]);

            }
        }

        tx.show(fragments[0]);

        tx.commit();

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.main_tab_bar);

        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index = 0;

        switch (checkedId){
            case R.id.main_tab_item_discover:
                index = 0;
                break;
            case R.id.main_tab_item_custom:
                index = 1;
                break;
            case R.id.main_tab_item_download:
                index = 2;
                break;
            case R.id.main_tab_item_personal:
                index = 3;
                break;
        }

        int length = fragments.length;

        FragmentManager manager = getSupportFragmentManager();

        FragmentTransaction tx = manager.beginTransaction();

        for (int i = 0; i < length; i++) {

            if(i == index){
                tx.show(fragments[i]);
            }else{
                tx.hide(fragments[i]);
            }
        }

        tx.commit();

    }
}
